package hevs.utils;

import hevs.greencontrol.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This class is for error management
 * @author Jonathan Moreira, Jun Yang, Natal Venetz, Ivan Samardziev
 *
 */
public class Error {
	
	/**
	 * Service function error management
	 * @param context
	 */
	public static void serviceFunctionError(Context context)
	{	
		AlertDialog alertDialog = new AlertDialog.Builder(context).create();
		alertDialog.setTitle("Service function error");
		alertDialog.setMessage("Service function error : "+ConnectionManager.getErrorMessage()+" Please try again later.");
		
		alertDialog.setButton("Ok", new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which)
			{
				
			}
		});

		alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
		alertDialog.show();
		
	}
	
	/**
	 * Toast for error/confirmation message 
	 * @param context
	 * @param msg
	 * @param level
	 */
	public static void toastNotif(Context context, String msg, int level)
	{
		
		LayoutInflater inflater = ((Activity)context).getLayoutInflater();
		View layout = inflater.inflate(R.layout.toast_layout,
		                               (ViewGroup) ((Activity)context).findViewById(R.id.toast_layout_root));
		
		ImageView image = (ImageView) layout.findViewById(R.id.image);
		
		switch(level)
		{
			case 1: 
		         image.setImageResource(android.R.drawable.ic_dialog_info);
		         break;
			case 2:
				 image.setImageResource(android.R.drawable.ic_dialog_alert);
		         break;
			case 3:
				image.setImageResource(R.drawable.ic_tab_check);
				break;
			case 4:
				image.setImageResource(R.drawable.ic_tab_cross);
				break;
			default :
				break;
		}
		
		TextView text = (TextView) layout.findViewById(R.id.text);
		text.setText(msg);
		
		
		Toast toast = new Toast(context);
		toast.setGravity(Gravity.BOTTOM, 0, 0);
		toast.setDuration(Toast.LENGTH_LONG);
		toast.setView(layout);
		toast.show();
		
	}
	
	/**
	 *Method used to manage no robot information error
	 * @param context
	 */
	public static void noRobotInfo(final Context context)
	{	
		AlertDialog alertDialog = new AlertDialog.Builder(context).create();
		alertDialog.setTitle("No robot registered");
		alertDialog.setMessage("Do you want to register a robot now?");
		
		alertDialog.setButton("Yes", new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which)
			{
				// to test
				// context.startActivity(new Intent(context,ListRobotTypes.class));
			}
			
		});
		
		alertDialog.setButton2("No", new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which)
			{
				 ((Activity)context).openOptionsMenu();
			}
		});

		alertDialog.setIcon(android.R.drawable.ic_dialog_info);
		alertDialog.show();
	}
	

}
